﻿using MyDiskInfoService.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.OracleClient;
using System.Diagnostics;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Forms;
using Utils;
using WCFServer;

namespace MyDiskInfo.Service
{
    /// <summary>
    /// 数据同步服务
    /// by suxiang
    /// </summary>
    partial class MyDiskInfoService : ServiceBase
    {
        #region 变量

        #endregion

        #region 构造函数
        public MyDiskInfoService()
        {
            InitializeComponent();
        }
        #endregion

        #region OnStart 启动服务
        protected override void OnStart(string[] args)
        {
            Task.Factory.StartNew(() =>
            {
                try
                {
                    OpenClientServer();

                    ServiceHelper.Get<CpuInfoUtil>().GetInfo();
                    ServiceHelper.Get<DiskInfoUtil>().GetInfo();
                }
                catch (Exception ex)
                {
                    LogUtil.Error(ex.Message + "\r\n" + ex.StackTrace);
                }
            });

            LogUtil.Log("服务启动成功");
        }
        #endregion

        #region OnStop 停止服务
        protected override void OnStop()
        {
            LogUtil.Log("服务停止成功");

            Thread.Sleep(100); //等待一会，待日志写入文件
        }
        #endregion

        #region Start 启动服务
        public void Start()
        {
            OnStart(null);
        }
        #endregion

        #region 启动WCF服务
        /// <summary>
        /// 启动WCF服务
        /// </summary>
        private void OpenClientServer()
        {
            NetNamedPipeBinding wsHttp = new NetNamedPipeBinding();
            wsHttp.MaxBufferPoolSize = 524288;
            wsHttp.MaxReceivedMessageSize = 2147483647;
            wsHttp.ReaderQuotas.MaxArrayLength = 6553600;
            wsHttp.ReaderQuotas.MaxStringContentLength = 2147483647;
            wsHttp.ReaderQuotas.MaxBytesPerRead = 6553600;
            wsHttp.ReaderQuotas.MaxDepth = 6553600;
            wsHttp.ReaderQuotas.MaxNameTableCharCount = 6553600;
            wsHttp.CloseTimeout = new TimeSpan(0, 1, 0);
            wsHttp.OpenTimeout = new TimeSpan(0, 1, 0);
            wsHttp.ReceiveTimeout = new TimeSpan(0, 10, 0);
            wsHttp.SendTimeout = new TimeSpan(0, 10, 0);
            wsHttp.Security.Mode = NetNamedPipeSecurityMode.None;

            Uri baseAddress = new Uri("net.pipe://localhost/diskInfoService");
            ServiceHost host = new ServiceHost(typeof(ClientServer), baseAddress);

            ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
            host.Description.Behaviors.Add(smb);

            ServiceBehaviorAttribute sba = host.Description.Behaviors.Find<ServiceBehaviorAttribute>();
            sba.MaxItemsInObjectGraph = 2147483647;

            host.AddServiceEndpoint(typeof(IClientServer), wsHttp, "");

            host.Open();
        }
        #endregion

    }
}
